package com.hskn.hss.module.grid.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.TreeEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.grid.To.GridTo;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Api(tags = {"-grid"}, description = "网格管理")
@Slf4j
@RestController
@RequestMapping("/hss/grid")
public class GridController {

    @Autowired
    private IGridService gridService;
    @Resource
    private GridMapper gridMapper;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            BaseEntity entity = new BaseEntity();
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = gridService.mList(entity, params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "电子围栏列表接口", notes = "电子围栏列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GridTo.class)
    })
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public AjaxResult<Object> getList(@RequestBody @Validated GridTo gridTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(gridService.getList(entity, gridTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "人车绑定电子围栏接口", notes = "人车绑定电子围栏")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = GridTo.class)
    })
    @RequestMapping(value = "getGridList", method = RequestMethod.POST)
    public AjaxResult<Object> getGridList(GridTo gridTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(gridService.getList(entity, gridTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "tree", method = RequestMethod.POST)
    public AjaxResult<Object> tree(@RequestBody @Validated GridTo gridTo) {
        try {
            List<TreeEntity> tree = gridService.tree(gridTo);
            return AjaxResult.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "grid", bean = Grid.class, beanMapper = GridMapper.class, names = "gridType,type,areaType")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public AjaxResult<Object> add(@RequestBody @Validated GridTo gridTo) {
        try {
            Grid grid = gridService.create(gridTo);

            return AjaxResult.success(grid);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "grid", idKey = "gridId", bean = Grid.class, beanMapper = GridMapper.class, names = "gridType,type,areaType")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(@RequestBody @Validated GridTo gridTo) {
        try {
            //作业网格禁用判断
            if(StringUtils.isNotEmpty(gridTo.getIsEnable()) && gridTo.getIsEnable().equals(0)){
                Integer check;
                check = gridService.schedulingPlanCheck(gridTo);
                if (check == 0){
                    return AjaxResult.fail("此网格有正在进行的作业，无法禁用");
                }
            }
            Grid grid = gridService.update(gridTo);
            return AjaxResult.success(grid);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "grid", idKey = "gridIds", bean = Grid.class, beanMapper = GridMapper.class, names = "gridType,type,areaType")
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public AjaxResult<Object> del(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(gridService.delete(params));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public AjaxResult<Object> detail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            Grid grid = gridService.detail(params);
            return AjaxResult.success(grid);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "checkGridList", method = RequestMethod.POST)
    public AjaxResult<Object> checkGridList(@RequestBody @Validated GridTo gridTo) {
        try {
            return AjaxResult.success(gridService.checkGridList(gridTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

//    @SysLog(businessType = BusinessType.UPDATE, tableName = "grid", idKey = "gridId", bean = Grid.class, beanMapper = GridMapper.class, names = "gridType,type,areaType")
    @RequestMapping(value = "updateAndInsert", method = RequestMethod.POST)
    public AjaxResult<Object> updateAndInsert(@RequestBody @Validated GridTo gridTo) {
        try {
            gridService.updateAndInsert(gridTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

//    @SysLog(businessType = BusinessType.UPDATE, tableName = "grid", idKey = "gridId", bean = Grid.class, beanMapper = GridMapper.class, names = "gridType,type,areaType")
    @RequestMapping(value = "updateAndInsert2", method = RequestMethod.POST)
    public AjaxResult<Object> updateAndInsert2(@RequestBody @Validated GridTo gridTo) {
        try {
            gridService.updateAndInsert2(gridTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

